package com.example.book_shop_java.controller;

import com.alibaba.fastjson.JSON;
import com.example.book_shop_java.bean.User;
import com.example.book_shop_java.dao.UserDao;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
public class LoginController {
    @Resource
    UserDao userDao;

    @RequestMapping("/userLogin")
    public String userLogin(@RequestParam("username") String username,
                            @RequestParam("password") String password){
        int i = userDao.userLogin(username,password);
        String message = "error";
        int code = 413;
        HashMap<String,Object> res = new HashMap<>();
        if(i > 0){
            List<User> user = userDao.getUserState(username,password);
            int State = user.get(0).getState();
            message = "success";
            code = 200;
            res.put("state",State);
        }
        res.put("message",message);
        res.put("code",code);

        String loginMessage = JSON.toJSONString(res);
        return loginMessage;
    }
}